/*
 * Copyright (c) 2005, the JUNG Project and the Regents of the University of
 * California All rights reserved.
 *
 * This software is open-source under the BSD license; see either "license.txt"
 * or http://jung.sourceforge.net/license.txt for a description.
 *
 * Created on Aug 23, 2005
 */
package com.davetrudes.jung.client.visualization.renderers;


import com.davetrudes.jung.client.algorithms.layout.Layout;
import com.davetrudes.jung.client.graph.Graph;
import com.davetrudes.jung.client.graph.util.Context;
import com.davetrudes.jung.client.graphics.Dimension;
import com.davetrudes.jung.client.graphics.Paint;
import com.davetrudes.jung.client.graphics.Rectangle;
import com.davetrudes.jung.client.graphics.Shape;
import com.davetrudes.jung.client.graphics.Stroke;
import com.davetrudes.jung.client.graphics.geom.AffineTransform;
import com.davetrudes.jung.client.graphics.geom.Point2D;
import com.davetrudes.jung.client.visualization.Layer;
import com.davetrudes.jung.client.visualization.RenderContext;
import com.davetrudes.jung.client.visualization.transform.MutableTransformer;
import com.davetrudes.jung.client.visualization.transform.MutableTransformerDecorator;
import com.davetrudes.jung.client.visualization.transform.shape.Graphics2DWrapper;

public class BasicVertexRenderer<V,E> implements Renderer.Vertex<V,E> {

    public void paintVertex(RenderContext<V,E> rc, Layout<V,E> layout, V v) {
    	Graph<V,E> graph = layout.getGraph();
        if (rc.getVertexIncludePredicate().evaluate(Context.<Graph<V,E>,V>getInstance(graph,v))) {
        	paintIconForVertex(rc, v, layout);
        }
    }

    /**
     * Paint <code>v</code>'s icon on <code>g</code> at <code>(x,y)</code>.
     */
    protected void paintIconForVertex(RenderContext<V,E> rc, V v, Layout<V,E> layout) {
        boolean vertexHit = true;
        // get the shape to be rendered
        Shape shape = rc.getVertexShapeTransformer().transform(v);

        Point2D p = layout.transform(v);
        p = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, p);
        float x = (float)p.getX();
        float y = (float)p.getY();
        // create a transform that translates to the location of
        // the vertex to be rendered
        AffineTransform xform = AffineTransform.getTranslateInstance(x,y);
        // transform the vertex shape with xtransform
        shape = xform.createTransformedShape(shape);

        vertexHit = vertexHit(rc, shape);
            //rc.getViewTransformer().transform(shape).intersects(deviceRectangle);

        if (vertexHit) {
        	paintShapeForVertex(rc, v, shape);
        }
    }

    protected boolean vertexHit(RenderContext<V,E> rc, Shape s) {
        MutableTransformer vt = rc.getMultiLayerTransformer().getTransformer(Layer.VIEW);
        if(vt instanceof MutableTransformerDecorator) {
        	vt = ((MutableTransformerDecorator)vt).getDelegate();
        }
        Dimension d = rc.getScreenDevice().getSize();
        return vt.transform(s).intersects(new Rectangle(0, 0, d.width, d.height));
    }

    protected void paintShapeForVertex(RenderContext<V,E> rc, V v, Shape shape) {
        Graphics2DWrapper g = rc.getGraphicsContext();
        Paint oldPaint = g.getPaint();
        Paint fillPaint = rc.getVertexFillPaintTransformer().transform(v);
        if(fillPaint != null) {
            g.setPaint(fillPaint);
            g.fill(shape);
            g.setPaint(oldPaint);
        }
        Paint drawPaint = rc.getVertexDrawPaintTransformer().transform(v);
        if(drawPaint != null) {
        	g.setPaint(drawPaint);
        	Stroke oldStroke = g.getStroke();
        	Stroke stroke = rc.getVertexStrokeTransformer().transform(v);
        	if(stroke != null) {
        		g.setStroke(stroke);
        	}
        	g.draw(shape);
        	g.setPaint(oldPaint);
        	g.setStroke(oldStroke);
        }
    }
}
